package cn.zhuzicc.basic.jvm6.t1;

/**
 * @author zhaoxiang
 * @version 1.0
 * @description jdk1.6环境测试
 * @Date 2021/9/19 21:18
 */
public class Demo1_21 {
    public static void main(String[] args) {
        // 分开执行，不然会影响结果
//       test1();
//       test2();
       test3();
    }

    public static void test1() {
        String s1 = "a";
        String s2 = "b";
        String s3 = "a" + "b";
        String s4 = s1 + s2;
        String s5 = "ab";
        String s6 = s4.intern();
        System.out.println(s3 == s4);
        System.out.println(s3 == s5);
        System.out.println(s3 == s6);
    }

    public static void test2() {
        String x2 = new String("c") + new String("d");
        /*
            串池："c"、"d"
            堆：new String("c")、new String("d")、new String("cd")
         */
        String x1 = "cd";
        /*
            串池："c"、"d"、"cd"
            堆：new String("c")、new String("d")、new String("cd")
         */
        x2.intern();

        System.out.println(x1 == x2);
    }

    public static void test3() {
        String x2 = new String("c") + new String("d");
        /*  串池："c"、"d"
            堆：new String("c")、new String("d")、new String("cd")
         */

        x2.intern();
          /*
            堆：new String("c")、new String("d")、new String("cd")
            串："c"、"d"、"cd"
         */
        String x1 = "cd";
         /*
            串池："cd"、c"、"d"
            堆：new String("c")、new String("d")、new String("cd")
         */
        System.out.println(x1 == x2);
    }
}
